package com.huibao.hulibao.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.example.zhouwei.library.CustomPopWindow;
import com.huibao.hulibao.R;
import com.huibao.hulibao.activity.LoginActivity;
import com.huibao.hulibao.activity.MainActivity;
import com.huibao.hulibao.activity.ServerDetailActivity;
import com.huibao.hulibao.activity.WebViewActivity;
import com.huibao.hulibao.adapter.KehuGvAdapter;
import com.huibao.hulibao.adapter.ServerLvAdapter;
import com.huibao.hulibao.constant.GlobalConstants;
import com.huibao.hulibao.entity.KeHuEntity;
import com.huibao.hulibao.entity.LoginEntity;
import com.huibao.hulibao.entity.ServerEntity;
import com.huibao.hulibao.utils.MyUtils;
import com.huibao.hulibao.utils.PreUtils;
import com.huibao.hulibao.utils.T;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.Unbinder;

import static com.huibao.hulibao.activity.MainActivity.*;

/**
 * Created by lizhaoyong on 2018/5/7.
 * 计划任务
 */
public class ServerFragmentView extends Fragment {
    private Unbinder unbinder;
    // 屏幕宽度
    public int width;

    private Context context;
    private MainActivity mainActivity;

    final Handler handler = new Handler();

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_title_arrow)
    ImageView iv_title_arrow;
    @BindView(R.id.iv_zhuangtai_arrow)
    ImageView iv_zhuangtai_arrow;
    @BindView(R.id.rl_zhuangtai)
    RelativeLayout rl_zhuangtai;
    @BindView(R.id.rl_kehu)
    RelativeLayout rl_kehu;
    @BindView(R.id.tv_quanbu)
    TextView tv_quanbu;
    @BindView(R.id.tv_zhuangtai)
    TextView tv_zhuangtai;
    @BindView(R.id.tv_kehu)
    TextView tv_kehu;
    @BindView(R.id.tv_server_head)
    TextView tv_server_head;
    @BindView(R.id.lv_server)
    ListView lv_server;
    @BindView(R.id.srl_server)
    SmartRefreshLayout srl_server;

    // Server数据模型
    private ServerEntity se;
    // 客户数据
    ArrayList<KeHuEntity> al_datas = new ArrayList<KeHuEntity>();
    // 客户筛选的id
    public String customer_id = "";
    public boolean customer_selected = false;

    // 客户选中的数据
    ArrayList<ServerEntity.ServerDetailData> filter_kehu = new ArrayList<ServerEntity.ServerDetailData>();
    // 记录客户第几个被选中
    public int kehu_selected_index = -1;
    // 服务中的选择条目
    public RelativeLayout rl_server;
    public RelativeLayout rl_store;
    public RelativeLayout rl_moon_dinner;
    // 状态的条目
    public RelativeLayout rl_weiwancheng;
    public RelativeLayout rl_daiqueren;
    public RelativeLayout rl_zhixingzhong;
    // pop中的gridview
    public GridView gv_kehu;
    // 记录用户选择
    public String filterCode = GlobalConstants.TYPE_ID_ALL;
    // 筛选记录 1是全部
    public String filterType = "1";
    public boolean secondQuanBu = true;
    // 执行中点击状态记录
    public boolean filter_zhixingzhong = false;
    // 登录成功后传递的对象
    private LoginEntity le;
    // 定时刷新
    public Runnable runnable;
    public boolean isWhile = false;
    public int REQUEST = 1101;

    // 弹出服务选择框的点击事件
    @OnClick({R.id.tv_title, R.id.iv_title_arrow})
    public void onClick() {
        showServerPop();
    }

    // 全部的点击事件
    @OnClick(R.id.tv_quanbu)
    public void onClickQuanbu() {
        secondQuanBu();
    }

    public void secondQuanBu() {
        secondQuanBu = true;
        customer_selected = false;
        customer_id = "";
        quanbuSelected();
        filterType = "1";
        tv_zhuangtai.setText(getResources().getString(R.string.zhuangtai));
        filter_zhixingzhong = false;
        tv_server_head.setVisibility(View.GONE);
        // 显示全部的数据
        if (se != null) {
            lv_server.setAdapter(new ServerLvAdapter(se.Message, context));
            lv_server.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    stopRefresh();
                    Intent intent = new Intent(context, ServerDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("ServerDetailData", se.Message.get(i));//序列化
                    intent.putExtras(bundle);//发送数据
                    startActivityForResult(intent, REQUEST);
                }
            });
        }
    }


    // 状态的点击事件
    @OnClick(R.id.rl_zhuangtai)
    public void onClickZhuangtai() {
        // 显示选择框
        showZhuangtaiPop();
    }

    // 客户的点击事件
    @OnClick(R.id.rl_kehu)
    public void onClickKehu() {
        showKehuPop();
    }


    // 服务记录的点击事件
    @OnClick(R.id.tv_fuwujilu)
    public void onClickFuWuJiLu() {
        Intent intent = new Intent(getActivity(), WebViewActivity.class);
        intent.putExtra("url", GlobalConstants.HOST + "AppWap/PDA_APP/WebTools/ShowServeRecords.aspx?ss=" + le.Message.Session);
        startActivity(intent);
    }

    // 状态被选中
    public void zhuangtaiSelected() {
        rl_zhuangtai.setBackground(getResources().getDrawable(R.drawable.corner_view_nurse_title_selected));
        tv_quanbu.setBackground(getResources().getDrawable(R.drawable.corner_view_nurse_title_no_selected));
    }

    // 全部被选中
    public void quanbuSelected() {
        rl_zhuangtai.setBackground(getResources().getDrawable(R.drawable.corner_view_nurse_title_no_selected));
        tv_quanbu.setBackground(getResources().getDrawable(R.drawable.corner_view_nurse_title_selected));
        // 客户显示效果处理
        tv_kehu.setText(getResources().getString(R.string.kehu));
        rl_kehu.setBackground(getResources().getDrawable(R.drawable.corner_view_nurse_title_no_selected));
    }

    //  服务popwindow的显示
    public void showServerPop() {


        // 箭头向下显示
        iv_title_arrow.setImageDrawable(getResources().getDrawable(R.mipmap.arrow_up));
        final CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(R.layout.popwindow_server)
                .setFocusable(true)
                .size(400, 500)
                .setOutsideTouchable(true)
                // pop消失监听
                .setOnDissmissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        iv_title_arrow.setImageDrawable(getResources().getDrawable(R.mipmap.arrow_down));
                    }
                })
                .create();
        // popwindow中的条目点击事件
        rl_server = (RelativeLayout) popWindow.getPopupWindow().getContentView().findViewById(R.id.rl_server);
        rl_store = (RelativeLayout) popWindow.getPopupWindow().getContentView().findViewById(R.id.rl_store);
        rl_moon_dinner = (RelativeLayout) popWindow.getPopupWindow().getContentView().findViewById(R.id.rl_moon_dinner);

        // 系统判断
        String sysType = (String) PreUtils.getObject(mainActivity, "SysType");
        if (sysType != null && sysType.equals("ck")) {
            rl_moon_dinner.setVisibility(View.GONE);
        }


        TextView tv_all = (TextView) popWindow.getPopupWindow().getContentView().findViewById(R.id.tv_all);

        tv_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_title.setText(getResources().getString(R.string.quanbu));
                popWindow.dissmiss();
                filterCode = GlobalConstants.TYPE_ID_ALL;
                getServerData(filterCode, true);
                secondQuanBu();
                tv_server_head.setVisibility(View.GONE);
                filter_zhixingzhong = false;
                mainActivity.closeDialog();
            }
        });

        rl_server.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_title.setText(getResources().getString(R.string.jihuafuwu));
                popWindow.dissmiss();
                filterCode = GlobalConstants.TYPE_ID_SERVER;
                getServerData(filterCode, true);
                secondQuanBu();
                tv_server_head.setVisibility(View.GONE);
                filter_zhixingzhong = false;
                mainActivity.closeDialog();
            }
        });

        rl_store.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_title.setText(getResources().getString(R.string.shangping));
                popWindow.dissmiss();
                filterCode = GlobalConstants.TYPE_ID_GOODS;
                getServerData(filterCode, true);
                secondQuanBu();
                tv_server_head.setVisibility(View.GONE);
                filter_zhixingzhong = false;
                mainActivity.closeDialog();
            }
        });

        rl_moon_dinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_title.setText(getResources().getString(R.string.yuezican));
                popWindow.dissmiss();
                filterCode = GlobalConstants.TYPE_ID_DINNER;
                getServerData(filterCode, true);
                secondQuanBu();
                tv_server_head.setVisibility(View.GONE);
                filter_zhixingzhong = false;
                mainActivity.closeDialog();
            }
        });
        // 设置显示位置
        popWindow.showAsDropDown(tv_title, -160, 20);
    }

    // 显示状态的popwindow
    public void showZhuangtaiPop() {
        // 箭头向下显示
        iv_zhuangtai_arrow.setImageDrawable(getResources().getDrawable(R.mipmap.arrow_up));
        final CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(R.layout.popwindow_zhuangtai)
                .setFocusable(true)
                .size(400, 400)
                .setOutsideTouchable(true)
                // pop消失监听
                .setOnDissmissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        iv_zhuangtai_arrow.setImageDrawable(getResources().getDrawable(R.mipmap.arrow_down));
                    }
                })
                .create();
        // popwindow中的条目点击事件
        rl_weiwancheng = (RelativeLayout) popWindow.getPopupWindow().getContentView().findViewById(R.id.rl_weiwancheng);
        rl_daiqueren = (RelativeLayout) popWindow.getPopupWindow().getContentView().findViewById(R.id.rl_daiqueren);
        rl_zhixingzhong = (RelativeLayout) popWindow.getPopupWindow().getContentView().findViewById(R.id.rl_zhixingzhong);

        rl_weiwancheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_zhuangtai.setText(getResources().getString(R.string.weiwancheng));
                filter_zhixingzhong = false;
                customer_selected = false;
                zhuangtaiSelected();
                popWindow.dissmiss();
                secondQuanBu = false;
                // 未完成
                filterType = GlobalConstants.STATUS_NO_FINISH;
                tv_server_head.setVisibility(View.VISIBLE);
                filtrStauts(filterType);

            }
        });

        rl_daiqueren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_zhuangtai.setText(getResources().getString(R.string.daiqueren));
                filter_zhixingzhong = false;
                customer_selected = false;
                zhuangtaiSelected();
                popWindow.dissmiss();
                secondQuanBu = false;
                // 待确认
                filterType = GlobalConstants.STATUS_NO_CONFIRM;
                tv_server_head.setVisibility(View.GONE);
                filtrStauts(filterType);
            }
        });

        rl_zhixingzhong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_zhuangtai.setText(getResources().getString(R.string.zhixingzhong));
                filterType = GlobalConstants.STATUS_RUNNING;
                filter_zhixingzhong = true;
                customer_selected = false;
                zhuangtaiSelected();
                popWindow.dissmiss();
                secondQuanBu = false;
                tv_server_head.setVisibility(View.GONE);
                // 通过本地记录筛选执行中
                filterZhiXingZhong();

            }
        });

        // 设置显示位置
        popWindow.showAsDropDown(tv_zhuangtai, -160, 20);
    }

    public void autoRefresh() {


        if (isWhile) {
            return;
        }
        isWhile = true;
        runnable = new Runnable() {
            @Override
            public void run() {
                // LogUtils.d("refresh");
                getServerData(filterCode, false);
                handler.postDelayed(this, 5000);
            }
        };

//        if (mainActivity.ServerHandler != null) {
//            mainActivity.ServerHandler.removeCallbacks(runnable);
//
//        }

        handler.postDelayed(runnable, 5000);

    }

    public void stopRefresh() {
        isWhile = false;
        handler.removeCallbacks(runnable);
    }


    // 显示客户的popwindow
    public void showKehuPop() {
        WindowManager wm = (WindowManager) mainActivity
                .getSystemService(Context.WINDOW_SERVICE);
        int hight = wm.getDefaultDisplay().getHeight();
        if (se != null) {
            final CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                    .setView(R.layout.popwindow_kehu)
                    .setFocusable(true)
                    .size(width, (int) (hight * 0.84))
                    .setOutsideTouchable(true)
                    // pop消失监听
                    .setOnDissmissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {

                        }
                    })
                    .create();
            // 客户gridview
            gv_kehu = (GridView) popWindow.getPopupWindow().getContentView().findViewById(R.id.gv_kehu);
            // 取消点击背景
            gv_kehu.setSelector(new ColorDrawable(Color.TRANSPARENT));
            // 初始化客户GRIDVIEW
            initKehuData(gv_kehu);
            // 点击事件
            gv_kehu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    tv_quanbu.setBackground(getResources().getDrawable(R.drawable.corner_view_nurse_title_no_selected));
                    rl_kehu.setBackground(getResources().getDrawable(R.drawable.corner_view_nurse_title_selected));
                    // 显示选中数据的样式
                    tv_kehu.setText(MyUtils.getKehuName(al_datas.get(i).CustomerName));
//                    rl_kehu.setBackground(getResources().getDrawable(R.drawable.corner_view_nurse_title_no_selected));
//                    tv_quanbu.setBackground(getResources().getDrawable(R.drawable.corner_view_nurse_title_selected));
                    // 记录选中数据的下标
                    kehu_selected_index = i;
                    popWindow.dissmiss();
                    secondQuanBu = false;
                    customer_selected = true;
                    // 筛选显示数据
                    filter_kehu.clear();
                    customer_id = al_datas.get(i).CustomerId;
                    int count = se.Message.size();
                    for (int j = 0; j < count; j++) {
                        if (se.Message.get(j).CustomerId.equals(customer_id)) {
                            filter_kehu.add(se.Message.get(j));
                        }
                    }
                    // LogUtils.d("filterType = " + filterType);
                    filtrStauts(filterType);
                    lv_server.setAdapter(new ServerLvAdapter(filter_kehu, context));
                    lv_server.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            stopRefresh();
                            Intent intent = new Intent(context, ServerDetailActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("ServerDetailData", filter_kehu.get(i));//序列化
                            intent.putExtras(bundle);//发送数据
                            startActivityForResult(intent, REQUEST);
                        }
                    });
                }
            });
            // 设置显示位置
            popWindow.showAsDropDown(tv_zhuangtai, 0, 30);
        }
    }


    // 筛选状态
    public void filtrStauts(String status) {
//        LogUtils.d("status = " + status);
//        LogUtils.d("customer_id = " + customer_id);
        filter_kehu.clear();
        String str = (String) PreUtils.getParam(mainActivity, "RenWuStatus", "");
        if (se != null && se.Message.size() > 0) {
            for (int i = 0; i < se.Message.size(); i++) {
                String current_status = se.Message.get(i).Status;
                LogUtils.d("current_status = " + current_status + "customer_id = " + se.Message.get(i).CustomerId);
                if (current_status.equals(status)) {
                    // 执行中过滤
                    if (!str.equals("")) {
                        if (!str.contains(se.Message.get(i).TaskId) && se.Message.get(i).CustomerId.equals(customer_id)) {
                            filter_kehu.add(se.Message.get(i));
                        }
                    } else {
                        // 如果用户筛选了客户
                        if (!customer_id.equals("")) {
                            if (se.Message.get(i).CustomerId.equals(customer_id)) {
                                filter_kehu.add(se.Message.get(i));
                            }
                        } else {
                            filter_kehu.add(se.Message.get(i));
                        }
                    }
                } else if (status.equals("1")) {
                    if (customer_selected && !customer_id.equals("") && se.Message.get(i).CustomerId.equals(customer_id)) {
                        filter_kehu.add(se.Message.get(i));
                    }
                    // 执行中
                } else if (status.equals(GlobalConstants.STATUS_RUNNING)) {
                    if (!str.contains(se.Message.get(i).TaskId) && se.Message.get(i).CustomerId.equals(customer_id)) {
                        filter_kehu.add(se.Message.get(i));
                    }
                }
            }

            if (status.equals(GlobalConstants.STATUS_NO_FINISH)) {
                tv_server_head.setText(context.getString(R.string.jintianhaiyou) + filter_kehu.size() + context.getString(R.string.geweiwanchengrenwu));
            }


            lv_server.setAdapter(new ServerLvAdapter(filter_kehu, context));

            lv_server.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    stopRefresh();
                    Intent intent = new Intent(context, ServerDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("ServerDetailData", filter_kehu.get(i));//序列化
                    intent.putExtras(bundle);//发送数据
                    startActivityForResult(intent, REQUEST);
                }
            });
        } else {
            tv_server_head.setVisibility(View.GONE);
        }
    }

    // 筛选执行中
    public void filterZhiXingZhong() {
        filter_kehu.clear();
        String str = (String) PreUtils.getParam(mainActivity, "RenWuStatus", "");
        if (str == null || str.equals("")) {
            lv_server.setAdapter(new ServerLvAdapter(filter_kehu, context));
        } else {
            if (se != null && se.Message.size() > 0) {
                for (int i = 0; i < se.Message.size(); i++) {
                    // String current_status = se.Message.get(i).TaskId;
                    if (str.contains(se.Message.get(i).TaskId)) {
                        // 如果用户筛选了客户
                        if (!customer_id.equals("")) {
                            if (se.Message.get(i).CustomerId.equals(customer_id)) {
                                filter_kehu.add(se.Message.get(i));
                            }
                        } else {
                            filter_kehu.add(se.Message.get(i));
                        }
                    }
                }
            }
            lv_server.setAdapter(new ServerLvAdapter(filter_kehu, context));
        }

        lv_server.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                stopRefresh();
                Intent intent = new Intent(context, ServerDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("ServerDetailData", filter_kehu.get(i));//序列化
                intent.putExtras(bundle);//发送数据
                startActivityForResult(intent, REQUEST);
            }
        });
    }


    // 初始化客户数据
    public void initKehuData(GridView gv) {
        al_datas.clear();
        ArrayList<ServerEntity.ServerDetailData> all_datas = se.Message;
        for (int i = 0; i < all_datas.size(); i++) {
            ServerEntity.ServerDetailData sdd = all_datas.get(i);
            if (al_datas.size() > 0) {
                if (isKeHuExist(sdd) == false) {
                    al_datas.add(new KeHuEntity(sdd.MetherName, sdd.CustomerId, sdd.RoomName));
                }
            } else {
                al_datas.add(new KeHuEntity(sdd.MetherName, sdd.CustomerId, sdd.RoomName));
            }
        }

        gv.setAdapter(new KehuGvAdapter(context, al_datas, kehu_selected_index));
    }


    public boolean isKeHuExist(ServerEntity.ServerDetailData sdd) {
        // 如果ID已存在就不重复存储
        for (int j = 0; j < al_datas.size(); j++) {
            if (sdd.CustomerId.equals(al_datas.get(j).CustomerId)) {
                return true;
            }
        }
        return false;
    }


    // 获取屏幕宽度
    public void getScreenWidth() {
        Activity activity = (Activity) context;
        WindowManager manager = activity.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        width = outMetrics.widthPixels;
    }

    // 获取计划任务数据
    public void getServerData(String typeid, Boolean b) {
        // 后台不请求
        if (MyUtils.isApplicationInBackground(mainActivity)) {
            return;
        }

        if (b) {
            mainActivity.showDialog();
        }
        // mainActivity.showDialog();
        RequestParams params = new RequestParams(GlobalConstants.URL);
        params.setCacheMaxAge(0);
        JSONObject object = new JSONObject();
        try {
            object.put("store", le.Message.StoreId);
            object.put("user", le.Message.UserId);
            object.put("typeid", typeid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.addBodyParameter("q", le.Message.Session + GlobalConstants.URL_SERVER_DATA + object.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    mainActivity.closeDialog();
                    JSONObject jsonObject = new JSONObject(result);
                    LogUtils.d("result = " + jsonObject.toString());
                    // 登录超时
                    if (jsonObject.toString().contains("\"State\"" + ":" + "\"" + GlobalConstants.LOGIN_TIME_OUT + "\"")) {
                        // 退出登录
                        PreUtils.saveObject(context, "LoginEntity", null);
                        Intent intent = new Intent(context, LoginActivity.class);
                        startActivity(intent);
                        getActivity().finish();
                        return;
                    }
                    se = GlobalConstants.gson.fromJson(jsonObject.toString(), ServerEntity.class);
                    // 如果没有计划任务了就清除之前记录的任务状态
//                    String status = (String) PreUtils.getParam(mainActivity, "RenWuStatus", "");
//                    if (!status.equals("")) {
//                        if (se.Message.size() == 0) {
//                            PreUtils.setParam(mainActivity, "RenWuStatus", "");
//                        }
//                    }
                    //LogUtils.d("status = " + PreUtils.getParam(mainActivity, "RenWuStatus", ""));
                    if (se.State.equals(GlobalConstants.SUCCESS_CODE)) {
                        // LogUtils.d("filterType = " + filterType);
                        srl_server.finishRefresh();
                        if (se.Message.size() > 0 && filterType.equals(GlobalConstants.STATUS_NO_FINISH)) {
                            tv_server_head.setText(context.getString(R.string.jintianhaiyou) + se.Message.size() + context.getString(R.string.geweiwanchengrenwu));
                            tv_server_head.setVisibility(View.VISIBLE);
                        } else {
                            tv_server_head.setVisibility(View.GONE);
                        }

                        // 处理筛选情况,设置适配器和点击事件
                        if (secondQuanBu) {
                            secondQuanBu();
                        } else {
                            if (filter_zhixingzhong) {
                                // 通过本地记录筛选执行中
                                filterZhiXingZhong();
                            } else {
                                filtrStauts(filterType);
                            }
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mainActivity.closeDialog();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                mainActivity.closeDialog();
            }

            @Override
            public void onFinished() {
                mainActivity.closeDialog();
            }
        });


    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_server, null);
        unbinder = ButterKnife.bind(this, v);
        context = getActivity();
        mainActivity = (MainActivity) getActivity();
        le = (LoginEntity) getActivity().getIntent().getSerializableExtra("LoginEntity");
        if (le == null) {
            le = (LoginEntity) PreUtils.getObject(context, "LoginEntity");
            if (le == null) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
            }
        }

        tv_server_head.requestFocus();

        // 测试本地序列号成功没有
//        LoginEntity le = (LoginEntity) PreUtils.getObject(context, "LoginEntity");
//        LogUtils.d("Session = " + le.Message.Session);
        if (le != null) {
            getServerData(filterCode, true);
        }

        // 下拉刷新监听
        srl_server.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                // T.show(context, "ReFresh", 0);
                // srl_server.finishRefresh();
                // getServerData(GlobalConstants.TYPE_ID_SERVER);
                getServerData(filterCode, true);
            }
        });

        // 获取屏幕宽度
        getScreenWidth();
        // 定时刷新
        autoRefresh();
        return v;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1101) {
            autoRefresh();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        stopRefresh();
    }
}
